﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;

namespace BiocityFilmquizAdmin.Src.DatabaseAccess
{

    public class QuizHelper
    {
        private DbAcces dbAccess;
        private static QuizHelper instance = null;

        private QuizHelper()
        {
            dbAccess = DbAcces.getInstance();
        }

        public static QuizHelper getInstance()
        {
            if (instance == null)
            {
                instance = new QuizHelper();
            }
            return instance;
        }

        public bool checkAnswer(int question, string answer)
        {
            String query = "SELECT * FROM questions WHERE question = " + question + " AND answer = '" + answer + "' and round = " + Statics.getInstance().getCurrentRound() + ";";

            MySqlDataReader reader = dbAccess.Select(query);

            bool answerResult = reader.HasRows;

            reader.Close();
            dbAccess.CloseConnection();

            return answerResult;
        }

        public String getAnswer(int question)
        {
            string query = "SELECT * FROM questions WHERE question = " + question + " and round = " + Statics.getInstance().getCurrentRound() + " order by questionsID;";
            MySqlDataReader reader = dbAccess.Select(query);

            reader.Read();

            String answer = reader.GetString(2);

            reader.Close();
            dbAccess.CloseConnection();

            return answer;
        }

        public void setCorrectAnswer(int question, int userId)
        {
            String query = "INSERT INTO answers (question, user, round) VALUES(" + question + ", " + userId + ", " + Statics.getInstance().getCurrentRound() + ");";

            dbAccess.Insert(query);
        }

        public void insertWrongAnswer(String answer, int question, int userId)
        {
            String query = "INSERT INTO wrongAnswers (answer, question, user, round) VALUES ('" + answer + "', " + question + ", " + userId + ", " + Statics.getInstance().getCurrentRound() + ");";
            dbAccess.Insert(query);
        }


    }


}